package com.example.stugradeapp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_add.*
import kotlinx.android.synthetic.main.activity_add.add_button
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        val user_name = ArrayList<String>()
        val user_pwd = ArrayList<String>()
        val myDB = MyDatabaseHelper(this)
        myDB.addUser()
        var tag = 0
        login_button.setOnClickListener{
            val curname = Id_input.text.toString()
            val curpwd  = pwd_input.text.toString()
            val cursor = myDB.readUser()
            if(cursor.columnCount > 0){
                while(cursor.moveToNext()){
                    user_name.add(cursor.getString(1))
                    user_pwd.add(cursor.getString(2))
                }
                for(i in 0..(user_name.size-1)){
                    if(curname == user_name[i] && curpwd == user_pwd[i]){
                        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show()
                        tag=1
                        val intent  = Intent(this,MainActivity::class.java)
                        startActivity(intent)
                    }
                }
            }
            if(tag==0){
                Toast.makeText(this, "账号或密码不正确", Toast.LENGTH_SHORT).show()
            }
        }
    }
}
//         val cursor = myDB.readData()
//        if(cursor.columnCount==0){
//            Toast.makeText(this,"no data",Toast.LENGTH_SHORT).show()
//        }else{
//            while(cursor.moveToNext()){
//                book_id.add(cursor.getString(0).toInt())
//                book_title.add(cursor.getString(1))
//                book_author.add(cursor.getString(2))
//                book_pages.add(cursor.getString(3).toInt())
//            }
//        }